package com.xtravel.common.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import com.xtravel.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ShareDialog {

    private AlertDialog dialog;
    private GridView gridView;
    private Button cancelButton;
    private SimpleAdapter saImageItems;
    private int[] image = {R.drawable.logo_sinaweibo, R.drawable.logo_wechat, R.drawable.logo_wechatmoments, R.drawable.logo_qq, R.drawable.logo_qzone};
    private String[] name = {"微博", "微信", "朋友圈", "QQ", "QQ空间"};
    private Context context;
    private OnClickListener cancelClickListener;
    private OnItemClickListener itemClickListener;

    public ShareDialog(Context context) {
        this.context = context;
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 非常重要：设置对话框弹出的位置
        window.setContentView(R.layout.share_platform_main);
        gridView = (GridView) window.findViewById(R.id.share_gridView);
        cancelButton = (Button) window.findViewById(R.id.share_cancel);
        List<HashMap<String, Object>> shareList = new ArrayList<>();
        for (int i = 0; i < image.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("ItemImage", image[i]);//添加图像资源的ID
            map.put("ItemText", name[i]);//按序号做ItemText
            shareList.add(map);
        }

        saImageItems = new SimpleAdapter(context, shareList, R.layout.share_platform_item, new String[]{"ItemImage", "ItemText"}, new int[]{R.id.imageView1, R.id.textView1});
        gridView.setAdapter(saImageItems);
        init();
    }

    public void show()
    {
        dialog.show();
    }

    private void init()
    {
        this.cancelButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                if(cancelClickListener != null)
                    cancelClickListener.onClick(view);
                dismiss();
            }
        });

        this.gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                if(itemClickListener != null)
                    itemClickListener.onItemClick(arg0, arg1, arg2, arg3);
                dismiss();
            }
        });
    }

    public void setCancelButtonOnClickListener(OnClickListener listener) {
        cancelClickListener = listener;
        //cancelButton.setOnClickListener(Listener);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        itemClickListener = listener;
        //gridView.setOnItemClickListener(listener);
    }

    public void dismiss() {
        dialog.dismiss();
    }
}